-------------------------------------------------------------------------------------
-- -*- coding: utf-8 -*-
--
-- @author:   zxf
-- @email:    07291@qq.com
-- @date:     2024/5/31
-- @module:   potion
-- @describe: 药品
-- @version:  v1.0
--

-------------------------------------------------------------------------------------

---@class potion
local potion = {}
local this = potion
---@type api
local api = import("api/api")
local quick_unit = quick_unit
local import = import
local main_ctx = main_ctx
---@type configer
local configer = import("base/configer")
---@type item
local item = import("game/func/item")
---@type potion_cfg
local potion_cfg = import("game/config/potion_cfg")
local QUICK_TABLE_CONFIG = potion_cfg.QUICK_TABLE_CONFIG
local QUICK_TABLE_CONFIG_AND_HP = potion_cfg.QUICK_TABLE_CONFIG_AND_HP
local ACTIVATED_POTION = potion_cfg.ACTIVATED_POTION
local ACTIVATED_POTION_IN_TASK = potion_cfg.ACTIVATED_POTION_IN_TASK
---@type actor_cfg
local actor_cfg = import("game/config/actor_cfg")
local JOB_INFO = actor_cfg.JOB_INFO

-- 是否需要设置药品快捷
function this.is_need_set_potion(IsSetHighHp, MyLevel, owner)
    local ret = {}
    local PotionConfig = {}
    local curJob = main_ctx:c_job()
    local jobInfo = JOB_INFO[curJob]
    local path = owner and owner.userPath or ""
    if not path or path == "" then
        local need_region = main_ctx:c_region_name()
        local need_server = main_ctx:c_server_name()
        local idx = this.get_role_idx()
        if idx ~= 0 then
            path = need_region .. "_" .. need_server .. "_" .. idx
        end
    end
    if not path or path == "" then
        return false, {}
    end
    if IsSetHighHp and MyLevel >= 99 then
        PotionConfig = QUICK_TABLE_CONFIG_AND_HP
    else
        PotionConfig = QUICK_TABLE_CONFIG
        if not table.is_empty(jobInfo) and jobInfo.food_name then
            table.insert(PotionConfig, 3, jobInfo.food_name)
        end
    end
    --print_r(PotionConfig)
    for k, v in ipairs(PotionConfig) do
        local isActivated = false
        local is_activated = configer.get_user_profile(path, v)
        if is_activated == "1" then
            isActivated = true
        end
        if not isActivated then
            local tmpItem = item.get_item_in_bag_by_name(0, v)
            if tmpItem then
                local isNeedSet = true
                for pos_quick = 4, 7 do
                    -- 如果快捷栏上有该药水则遍历下一个药水
                    if quick_unit.get_quick_item_id(pos_quick) == tmpItem.res_id then
                        isNeedSet = false
                        break
                    end
                end
                if isNeedSet then
                    table.insert(ret, tmpItem)
                end
            end
        end
    end
    potion.write_activated_potion_to_ini(owner)
    if #ret > 0 then
        --local list = api.get_item_info(0)
        --local notItemList = {}
        --for idx = 4, 7 do
        --    local res_id = quick_unit.get_quick_item_id(idx)
        --    if res_id ~= 0 then
        --        if quick_unit.quick_is_active(idx) then
        --            local isFind = false
        --            for k, info in ipairs(list) do
        --                -- 通过res_id找到快捷栏上的药品
        --                if info.res_id == res_id then
        --                    isFind = true
        --                    break
        --                end
        --            end
        --            if not isFind then
        --                table.insert(notItemList, { idx = idx })
        --            end
        --        end
        --    else
        --        table.insert(notItemList, { idx = idx })
        --    end
        --end
        --if table.is_empty(notItemList) then
        --    table.insert(notItemList, { idx = 6 })
        --end
        --print_r(ret)
        return true, ret
    end
    return false
end

-- 是否需要设置药品快捷
function this.is_need_set_potion_and_high_hp()
    local ret = {}
    for k, v in ipairs(QUICK_TABLE_CONFIG_AND_HP) do
        local tmpItem = item.get_item_in_bag_by_name(0, v)
        if tmpItem then
            local isNeedSet = true
            for pos_quick = 4, 7 do
                -- 如果快捷栏上有该药水则遍历下一个药水
                if quick_unit.get_quick_item_id(pos_quick) == tmpItem.res_id then
                    isNeedSet = false
                    break
                end
            end
            if isNeedSet then
                table.insert(ret, tmpItem)
            end
        end
    end
    if #ret > 0 then
        return true, ret
    end
    return false
end

-- 获取快捷栏上激活的药品
function potion.get_activated_potion_in_quick()
    local list = api.get_item_info(0)
    if table.is_empty(list) then
        return false, {}, {}
    end
    local tmpList = {}
    local notItemList = {}
    for idx = 4, 7 do
        local res_id = quick_unit.get_quick_item_id(idx)
        if res_id ~= 0 then
            if quick_unit.quick_is_active(idx) then
                local isFind = false
                for k, info in ipairs(list) do
                    -- 通过res_id找到快捷栏上的药品
                    if info.res_id == res_id then
                        isFind = true
                        table.insert(tmpList, info)
                        break
                    end
                end
                if not isFind then
                    table.insert(notItemList, { idx = idx })
                end
            end
        else
            table.insert(notItemList, { idx = idx })
        end
    end
    if table.is_empty(tmpList) then
        return false, {}, notItemList
    end
    return true, tmpList, notItemList
end

function this.get_role_idx(owner)
    local myName = owner and owner.name or local_player:name()
    local list = api.get_role_info()
    local idx = 0
    for k, v in ipairs(list) do
        if v.name == myName then
            idx = k
            break
        end
    end
    return idx
end

-- 写入快捷栏上已激活的药物于ini
function potion.write_activated_potion_to_ini(owner)
    local need_region = main_ctx:c_region_name()
    local need_server = main_ctx:c_server_name()
    local idx = this.get_role_idx()
    local path = owner and owner.userPath or (idx ~= 0 and need_region .. "_" .. need_server .. "_" .. idx)
    if not path then
        return false
    end
    local is_get, list = potion.get_activated_potion_in_quick()
    if not is_get then
        return false
    end
    for k, v in ipairs(list) do
        local name = v.name
        if string.find(name, "（归属）") then
            name = string.gsub(name, "（归属）", "")
        end
        configer.set_user_profile(path, name, 1)
        --local str1 = "守卫灵药"
        if string.find(name, '【活动】') then
            -- 设置非活动的药水为0
            local potion_name = string.gsub(name, "【活动】", "")
            local potion_ini = configer.get_user_profile(path, potion_name)
            if potion_ini and potion_ini == "1" then
                configer.set_user_profile(path, potion_name, 0)
            end
        else
            -- 设置活动的药水为0
            local potion_name = string.format("【活动】%s", name)
            local potion_ini = configer.get_user_profile(path, potion_name)
            if potion_ini and potion_ini == "1" then
                configer.set_user_profile(path, potion_name, 0)
            end
        end
    end
end

-- 根据ini获取需要激活的药品
function potion.get_need_activated_potion_in_ini(owner)
    local need_region = main_ctx:c_region_name()
    local need_server = main_ctx:c_server_name()
    local idx = this.get_role_idx()
    local path = owner and owner.userPath or need_region .. "_" .. need_server .. "_" .. idx
    local list = {}
    if not path then
        return false, {}
    end

    for k, name in ipairs(ACTIVATED_POTION) do
        local is_activated = configer.get_user_profile(path, name)
        if not is_activated or is_activated ~= "1" then
            table.insert(list, name)
        end
    end
    local tmpList = {}

    if not table.is_empty(list) then
        local bag_list = api.get_item_info(0)
        if not table.is_empty(bag_list) then
            for _, v in ipairs(list) do
                for _, info in ipairs(bag_list) do
                    local name = info.name
                    if not info.is_expiration then
                        if string.find(name, v) then
                            table.insert(tmpList, info)
                            break
                        end
                    end
                end
            end
        end
        if not table.is_empty(tmpList) then
            local notItemList = {}
            for quick_idx = 4, 7 do
                local res_id = quick_unit.get_quick_item_id(quick_idx)
                if res_id ~= 0 then
                    if quick_unit.quick_is_active(quick_idx) then
                        local isFind = false
                        for k, info in ipairs(bag_list) do
                            -- 通过res_id找到快捷栏上的药品
                            if info.res_id == res_id then
                                isFind = true
                                break
                            end
                        end
                        if not isFind then
                            table.insert(notItemList, { idx = quick_idx })
                        end
                    end
                else
                    table.insert(notItemList, { idx = quick_idx })
                end
            end
            --print_r(tmpList)
            --print_r(notItemList)
            return true, tmpList, notItemList
        end
    end
    return false, {}, {}
end

return this
